import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;

public class HDFSClient {
    FileSystem fs = null;

    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://10.211.55.8:9000");
        conf.set("dfs.replication", "3");
        fs = FileSystem.get(new URI("hdfs://10.211.55.8:9000"), conf, "root");
    }

    @Test
    public void testAddFileToHDFS() throws Exception {
        Path src = new Path("src/input/test.txt");
        Path dst = new Path("/test");
        fs.copyFromLocalFile(src, dst);
        fs.close();
    }

    @Test
    public void testDownFileToLocal() throws IllegalArgumentException, IOException {
        Path src = new Path("/test/test.txt");
        Path dst = new Path("src/ouput/");
        fs.copyToLocalFile(src, dst);
        fs.close();
    }

    @Test
    public void testMkdirAndDeleteAndRename() throws IOException {
        fs.mkdirs(new Path("/input/testMkdir"));
//        fs.delete(new Path("/input/testMkdir"));
        fs.rename(new Path("/input/testMkdir"), new Path("/input/testRename"));
        fs.close();
    }

    @Test
    public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {

// 思考：为什么返回迭代器，而不是 List 之类的容器

        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while (listFiles.hasNext()) {

            LocatedFileStatus fileStatus = listFiles.next();

            System.out.println(fileStatus.getPath().getName());

            System.out.println(fileStatus.getBlockSize());

            System.out.println(fileStatus.getPermission());

            System.out.println(fileStatus.getLen());

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation bl : blockLocations) {

                System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());

                String[] hosts = bl.getHosts();

                for (String host : hosts) { System.out.println(host);

                }

            }

            System.out.println("---------------------------");

        }

    }

    @Test
    public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {



        FileStatus[] listStatus = fs.listStatus(new Path("/"));



        String flag = "d--             ";

        for (FileStatus fstatus : listStatus) {

            if (fstatus.isFile())  flag = "f--         ";

            System.out.println(flag + fstatus.getPath().getName());

        }

    }

}
